﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Controls;
using Microsoft.HomeServer.Extensibility;
using Microsoft.HomeServer.HomeServerConsoleTab.TV4Home.Server.WHS.AddIn.TabPageControls;

namespace Microsoft.HomeServer.HomeServerConsoleTab.TV4Home.Server.WHS.AddIn
{
    public partial class MainTabUserControl : UserControl
    {
        private IConsoleServices consoleServices;
        private InstallationTabUserControl installUserCtrl;
        private SetupTVTabUserControl setupTVUserCtrl;
        private StatusTabUserControl statusUserCtrl;
        private System.Diagnostics.Process devMgrProcess;

        public MainTabUserControl()
        {
            InitializeComponent();
        }

        public MainTabUserControl(int width, int height, IConsoleServices consoleServices)
            : this()
        {
            this.Width = width;
            this.Height = height;
            this.consoleServices = consoleServices;

            this.ctbInstall.Image = Microsoft.HomeServer.Controls.CommonSettings.SoftwareIcon32x32.ToBitmap();
            this.ctbStatus.Image = Microsoft.HomeServer.Controls.CommonSettings.SettingsIcon;
            this.ctbSetupTV.Image = Properties.Resources.SetupTv.ToBitmap();
            this.ctbDevMgr.Image = Microsoft.HomeServer.Controls.CommonSettings.ServerHardwareIcon.ToBitmap();
            this.ctbCheckForUpdates.Image = Microsoft.HomeServer.Controls.CommonSettings.StatusHealthy16.ToBitmap();
            this.ctbAbout.Image = Microsoft.HomeServer.Controls.CommonSettings.InformationImage24x24;

            installUserCtrl = new InstallationTabUserControl() { Dock = DockStyle.Fill };
            statusUserCtrl = new StatusTabUserControl() { Dock = DockStyle.Fill };
            setupTVUserCtrl = new SetupTVTabUserControl() { Dock = DockStyle.Fill };

            this.pnViewScreen.Controls.Add(installUserCtrl);
            this.pnViewScreen.Controls.Add(statusUserCtrl);
            this.pnViewScreen.Controls.Add(setupTVUserCtrl);

            if (Utils.ComponentVerifier.CheckTVServer().Status == Utils.ComponentVerifier.VerificationStatus.NotInstalled)
                SwitchViewScreen(ViewScreens.Home);
            else
                SwitchViewScreen(ViewScreens.Status);
        }

        private void SwitchViewScreen(ViewScreens screen)
        {
            switch (screen)
            {
                case ViewScreens.Home:
                    break;

                case ViewScreens.Status:
                    break;

                case ViewScreens.SetupTV:

                    if (Utils.ComponentVerifier.CheckTVServer().Status == Utils.ComponentVerifier.VerificationStatus.NotInstalled)
                    {
                        MessageBox.Show("TV Server is not installed!");
                        return;
                    }

                    if (!this.setupTVUserCtrl.IsInitialized)
                        setupTVUserCtrl.Initialize();

                    break;

                default:
                    throw new ArgumentOutOfRangeException("screen");
            }

            ShowViewScreenAndCheckTabButton(screen);
        }

        private void ShowViewScreenAndCheckTabButton(ViewScreens screen)
        {
            // check correct tab button
            this.ctbInstall.Checked = screen == ViewScreens.Home;
            this.ctbStatus.Checked = screen == ViewScreens.Status;
            this.ctbSetupTV.Checked = screen == ViewScreens.SetupTV;

            // show correct view screen
            this.installUserCtrl.Visible = screen == ViewScreens.Home;
            this.statusUserCtrl.Visible = screen == ViewScreens.Status;
            this.setupTVUserCtrl.Visible = screen == ViewScreens.SetupTV;
        }

        private void ctbHome_Click(object sender, EventArgs e)
        {
            SwitchViewScreen(ViewScreens.Home);
        }

        private void ctbStatus_Click(object sender, EventArgs e)
        {
            SwitchViewScreen(ViewScreens.Status);
        }

        private void ctbSetupTV_Click(object sender, EventArgs e)
        {
            SwitchViewScreen(ViewScreens.SetupTV);
        }

        private void ctbDevMgr_Click(object sender, EventArgs e)
        {
            StartDeviceManager();
        }

        private void ctbCheckForUpdates_Click(object sender, EventArgs e)
        {
            new CheckForUpdatesForm().Run();
        }

        private void ctbAbout_Click(object sender, EventArgs e)
        {
            new AboutForm().Run();
        }

        private void StartDeviceManager()
        {
            // check if devmgr is still running (-> then bring it to front), else start a new instance
            if (devMgrProcess == null || devMgrProcess.HasExited)
            {
                if (DialogResult.OK == QMessageBox.Show("Please be careful when using the Device Manager. Improper usage of system tools may render your server unusable.",
                                                        "Caution",
                                                        MessageBoxButtons.OKCancel,
                                                        MessageBoxIcon.Warning))
                    devMgrProcess = System.Diagnostics.Process.Start("devmgmt.msc");
            }
            else
            {
                devMgrProcess.Refresh();

                // restore the window (e.g. if it was minimized)
                Utils.Win32.ShowWindow(devMgrProcess.MainWindowHandle, Utils.Win32.SW_RESTORE);

                // bring the window to the front
                Utils.Win32.SetForegroundWindow(devMgrProcess.MainWindowHandle);
            }
        }

        enum ViewScreens
        {
            Home,
            Status,
            SetupTV
        }
    }
}
